package com.ithzl.capacity.mapper;

import com.ithzl.capacity.pojo.EntryForm;
import com.ithzl.capacity.pojo.TeamQualification;
import com.ithzl.capacity.pojo.TeamWorks;
import com.ithzl.capacity.pojo.dto.*;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface EntryFormMapper {


    /**
     * 添加记录
     * @param entryForm
     */
    void add(@Param("entryForm") EntryForm entryForm);

    /**
     * 设置教师团队资格
     * @param teamQualification
     */
    @Insert("insert into team_qualification(entry_form_id, qualification_number, qualification_name) " +
            "values (#{teamQualification.entryFormId},#{teamQualification.qualificationNumber},#{teamQualification.qualificationName})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    void setTeamQualification(@Param("teamQualification") TeamQualification teamQualification);


    /**
     * 设置作品中团队的编号例属
     * @param entryFormId
     * @param teamNumber
     */
    @Insert("insert into entry_team(entry_form_id,team_number) values (#{entryFormId},#{teamNumber})")
    void addEntryTeam(@Param("entryFormId") int entryFormId, @Param("teamNumber") String teamNumber);

    /**
     * 添加团队作品
     * @param teamWorks
     */
    @Insert("insert into team_works values (#{teamWorks.teamNumber},#{teamWorks.works},#{teamWorks.worksName},#{teamWorks.worksType})")
    void addTeamWorks(@Param("teamWorks") TeamWorks teamWorks);

    /**
     * 查找是否已经有该记录
     * @param entryFormId
     * @param teamNumber
     * @return
     */
    @Select("select * from entry_team where entry_form_id=#{entryFormId} and team_number=#{teamNumber}")
    Integer selectEntryTeam(@Param("entryFormId") int entryFormId, @Param("teamNumber") String teamNumber);

    /**
     * 更新报名表
     * @param entryForm
     */
    void updateEntryTeam(@Param("entryForm") EntryForm entryForm);

    /**
     * 查找团队教学资格是否有
     * @param teamQualification
     * @return
     */
    @Select("select * from team_qualification where id=#{teamQualification.id}")
    int selectTeamQualification(@Param("teamQualification") TeamQualification teamQualification);


    @Delete("delete from team_qualification where id=#{teamQualification.id}")
    void deleteTeamQualification(@Param("teamQualification") TeamQualification teamQualification);

    @Select("select id from entry_team where entry_form_id=#{entryFormId} and team_number=#{teamNumber}")
    int selectEntryTeamGetId(@Param("entryFormId") int entryFormId, @Param("teamNumber") String teamNumber);

    @Delete("delete from entry_team where team_number=#{teamNumber}")
    void deleteEntryTeam(@Param("teamNumber") String teamNumber);

    @Select("select * from team_works where team_number=#{teamNumber}")
    int selectTeamWorks(@Param("teamNumber") String teamNumber);

    @Delete("delete from team_works where team_number=#{teamNumber}")
    void deleteTeamWorks(@Param("teamNumber") String teamNumber);

    /**
     * 找出所有该报名表的团队编号
     * @param entryFormId
     * @return
     */
    @Select("select team_number from entry_team where entry_form_id=#{entryFormId}")
    List<String> selectTeamNumberByEntryFormId(@Param("entryFormId") int entryFormId);

    /**
     * 提交报名表
     * @param id
     */
    @Update("update entry_form set submit=1 where entry_form_id=#{id}")
    void submit(@Param("id") int id);

    /**
     * 添加审评
     * @param id
     * @param opinion
     */
    @Insert("insert into opinion values (#{id},#{opinion})")
    void review(@Param("id") int id,@Param("opinion") String opinion);

    /**
     * 修改分数
     * @param id
     * @param score
     */
    @Update("update entry_form set score=#{score},submit=2 where entry_form_id=#{id}")
    void updateScore(@Param("id") int id,@Param("score") double score);

    /**
     * 分页查找报名表的简要信息
     * @param reviewPageDTO
     * @return
     */
    List<WorksBriefDTO> selectEntryFormBrief(@Param("reviewPageDTO") ReviewPageDTO reviewPageDTO);

    /**
     * 查询报名表单
     * @param entryFormId
     * @return
     */
    @Select("select * from entry_form where entry_form_id=#{entryFormId}")
    EntryForm selectEntryForm(@Param("entryFormId") int entryFormId);

    /**
     * 获取该报名表的教学资格
     * @param entryFormId
     * @return
     */
    @Select("select * from team_qualification where entry_form_id=#{entryFormId}")
    List<TeamQualification> getTeamQualificationList(@Param("entryFormId") int entryFormId);

    /**
     * 查询该表中的团队作品
     * @param entryFormId
     * @return
     */
    @Select("select * from team_works where team_number IN (select team_number from entry_team where entry_form_id=#{entryFormId})")
    List<TeamWorks> selectTeamWorksList(@Param("entryFormId") int entryFormId);

    /**
     * 添加作品评语
     * @param entryFormId
     * @param opinion
     */
    @Insert("insert into opinion value (#{entryFormId},#{userId},#{opinion})")
    void instOpinion(@Param("entryFormId") int entryFormId,@Param("opinion") String opinion,@Param("userId") int userId);

    /**
     * 查询作品列表
     * @param worksPageDTO
     * @return
     */
    List<WorksControlBrief> selectEntryTeamList(@Param("worksPageDTO") WorksPageDTO worksPageDTO);

    /**
     * 查询评价简略信息
     * @param evaluatePageDTO
     * @return
     */
    List<EvaluateBriefDTO> selectEvaluateList(@Param("evaluatePageDTO") EvaluatePageDTO evaluatePageDTO);

    /**
     * 获得表单的评审内容
     * @param entryFormId
     * @return
     */
    @Select("select name as judgeName ,user_id as judgeUserId,opinion " +
            "from opinion join user u on opinion.user_id = u.id " +
            "where entry_form_id=#{entryFormId}")
    Map<String, String> selectOpinion(@Param("entryFormId") int entryFormId);
}
